import {takeEvery, takeLatest, delay, END} from 'redux-saga';
import {put, call, take, fork, cancel, cancelled, all} from 'redux-saga/effects';

import {
    queryUserList,
    createUser,
} from '../utils/Api';

import {
    SEARCH_USER,
    REQUEST_USERS,
    RECEIVE_USERS,
    CREATE_USER,
    CREATE_REQUEST_USER,
    CREATE_RECEIVE_USER,
} from '../store/actions/userAction';




function* watchQueryUser() {

    while (true) {
        const data = yield take(SEARCH_USER, queryUserEffects);
        yield fork(queryUserEffects, data)
    }
}

function* queryUserEffects(action) {
    yield put({type: REQUEST_USERS, action});
    const data = yield call(queryUserList, {
        name: action.name,
        account: action.account,
        email: action.email,
        pageNum: action.pageNum,
        pageSize: action.pageSize,
    });
    yield put({type: RECEIVE_USERS, data});
}



function* watchCreateUser() {

    while (true) {
        const data = yield take(CREATE_USER, createEffects);
        yield fork(createEffects, data);
    }
}

function* createEffects(action) {
    yield put({type: CREATE_REQUEST_USER, action});
    const data = yield call(createUser, action);
    yield put({type: CREATE_RECEIVE_USER, data});
}



export default function* rootSaga() {

    yield all([
        fork(watchQueryUser),
        fork(watchCreateUser),
    ]);

}
